package com.srs.dao.cfg;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.cfg.Province;
import com.srs.util.HibernateUtil;

public class ProvinceDao {

	final static Logger logger = LoggerFactory.getLogger(ProvinceDao.class);

	private List<Province> provinceList;
	private Province province;

	public void delete(Integer provinceId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			province = (Province) session.get(Province.class, provinceId);
			session.delete(province);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Province> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			provinceList = session.createQuery("from Province order by provinceName").list();
			return provinceList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<Province> list(Integer areaId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// Get list of Agent
			Query q = session.createQuery("select a.provinceId from Agent a where a.areaId=:areaId");
			q.setInteger("areaId", areaId);
			List<Integer> areas = q.list();
			for(int i=0; i<areas.size(); i++){
				for(int j=areas.size()-1; j>i; j--){
					if (areas.get(i).intValue()==areas.get(j).intValue()){
						areas.remove(j);
					}
				}
			}
			String inCondition = " in (" + areas.get(0).toString();
			for (int i=1; i<areas.size(); i++) {
				inCondition = inCondition + ", " + areas.get(i).toString() ;
			}
			inCondition = inCondition + ")";
			
			provinceList = session.createQuery("from Province p where p.provinceId " + inCondition + " order by p.provinceName").list();
			return provinceList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}	
	
	public Province get(Integer provinceId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from Province p where p.provinceId=:provinceId");
			q.setInteger("provinceId", provinceId);
			return (Province) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public void insert(Province province) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(province);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(Province province) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(province);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}
	public static void main(String[] args) {
		// Init sessionFactory
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		HibernateUtil.setSessionFactory(sessionFactory);
		
		// Get list params
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		
		Query q = session.createQuery("select a.provinceId from Agent a where a.areaId=:areaId");
		q.setInteger("areaId", 12);
		List<Integer> areas = q.list();
		for(int i=0; i<areas.size(); i++){
			for(int j=areas.size()-1; j>i; j--){
				if (areas.get(i).intValue()==areas.get(j).intValue()){
					areas.remove(j);
				}
			}
		}
		String inCondition = " in (" + areas.get(0).toString();
		for (int i=1; i<areas.size(); i++) {
			inCondition = inCondition + ", " + areas.get(i).toString() ;
		}
		inCondition = inCondition + ")";
		
		List<Province> provinceList = session.createQuery("from Province p where p.provinceId " + inCondition).list();
		System.out.println(provinceList);
	} 
}
